package com.zhsource.auth;


import cn.hutool.core.util.ObjUtil;
import com.zhsource.system.domain.Logininfo;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class SecurityUserDetails implements UserDetails {

    private Logininfo logininfo;

    private List<String> permissionSns;

    public SecurityUserDetails(Logininfo logininfo, List<String> permissionSns) {
        this.logininfo = logininfo;
        this.permissionSns = permissionSns;
    }

    /**
     * security要求在你登录的时候把这个用户的所有权限也交给他
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (ObjUtil.isEmpty(this.permissionSns)){
            return new ArrayList<SimpleGrantedAuthority>();
        }
        return this.permissionSns.stream().map(SimpleGrantedAuthority::new).toList();
    }

    @Override
    public String getPassword() {
        return logininfo.getPassword();
    }

    @Override
    public String getUsername() {
        return logininfo.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
